<?php
/**
 * Created by PhpStorm.
 * User: sjeam
 * Date:2019年11月7日 17:22:37
 */
namespace app\modules\content\controllers;

use app\libs\Method;


use app\modules\content\models\ParentUrl;
// use app\modules\content\models\PositionBlock;


use app\libs\ContentControl;
 



use Yii;
use yii\data\Pagination;

class BlockController extends ContentControl{
    public $enableCsrfValidation = false;



    //权限修改
    public function actionIndex(){
        // $block = Yii::$app->params['block'];

        // foreach($block as $key=> $v){
        //     $block[$key]['position']= Position::find()->where("belong = '{$v['name']}' ")->asArray()->all();
        // }

        $block = ParentUrl::find()->orderBy("sort,id")->asArray()->all();

        // var_dump($block);die;
        // $this->layout = false;
        return $this->render('index',['block'=>$block]);
    }

 

    // // 权限修改--详情
    // public function actionAddBlock(){
    //     $data = Yii::$app->request->post('data');
    //     $id = Yii::$app->request->post('id');
    //     $name = Yii::$app->request->post('name');
    //     $belong = Yii::$app->request->post('belong');
    //         // var_dump($data);die;
    //     if($id){
    //         // Position::deleteAll(['id'=>$id]);
    //         PositionBlock::deleteAll(['position'=>$id]);
    //         $Position=  Position::find()->where("id=$id")->one();
    //         $Position->name=$name;
    //         $Position->save();
    //     }else{
    //         $Position= new Position();
    //         $Position->name=$name;
    //         $Position->belong=$belong;
    //         $Position->save();
    //         $id = $Position->primaryKey;
    //     }
    //     foreach($data as $v){
    //       $PositionBlock=   new PositionBlock();
    //       $PositionBlock->position=$id;
    //       $PositionBlock->blockId=$v;
    //       $PositionBlock->save();
    //     }
    //     // var_dump($data);die;
    //     die(Method::jsonGenerate(1,null,'success'));
    // }


    // 权限修改--详情
    public function actionUpdate(){
        $id = Yii::$app->request->post('id');
        $name = Yii::$app->request->post('name');
        $url = Yii::$app->request->post('url');
        $sort = Yii::$app->request->post('sort');
        $type = Yii::$app->request->post('type');
        $parent = ParentUrl::find()->where("id =$id")->one();
        $parent ->name=$name;
        $parent ->url=$url;
        $parent ->sort=$sort;
        $parent ->type=$type;
        $parent ->save();
        // ParentUrl::deleteAll(['id'=>$id]);
        // PositionBlock::deleteAll(['position'=>$id]);
        die(Method::jsonGenerate(1,null,'success'));
    }
    // 权限修改--详情
    public function actionDelete(){
        $id = Yii::$app->request->post('id');
        ParentUrl::deleteAll(['id'=>$id]);
        // PositionBlock::deleteAll(['position'=>$id]);
        die(Method::jsonGenerate(1,null,'success'));
    }
    // 权限修改--详情
    public function actionAdd(){
        $id = Yii::$app->request->post('id');
        // ParentUrl::deleteAll(['id'=>$id]);
        $parent = new ParentUrl();
        $parent ->name='';
        $parent ->url='';
        $parent ->type=1;
        $parent ->sort= 0;
        $parent->save();
        $data=  ParentUrl::find()->orderBy("id desc")->asArray()->one();
        // PositionBlock::deleteAll(['position'=>$id]);
        die(Method::jsonGenerate(1,$data,'success'));
    }

}